//
//  MoreCompanyVC.m
//  HNJYB
//
//  Created by panshen on 2017/10/25.
//  Copyright © 2017年 Mag1cPanda. All rights reserved.
//

#import "MoreCompanyVC.h"
#import "BXBAView.h"
#import "OneTipsViewController.h"

@interface MoreCompanyVC ()
{
    UIScrollView *scroll;
    BXBAView *bfView;
    BXBAView *dfView;
    
    NSString *inscomcode;
}
@end

@implementation MoreCompanyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"保险理赔";
    
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64)];
    bfView = [[NSBundle mainBundle] loadNibNamed:@"BXBAView" owner:nil options:nil][0];
    bfView.frame = CGRectMake(0, 0, ScreenWidth, 370);
    bfView.title.text = @"本方保险信息";
    bfView.listView.dataArr = _comArr;
    [scroll  addSubview:bfView];
    
    //本方信息
//    NSDictionary *tmpDic0 = GlobleInstance.casecarlist[0];
    NSDictionary *tmpDic0;
    for (NSDictionary *dic in GlobleInstance.casecarlist) {
        if ([dic[@"party"] isEqualToString:@"1"]) {
            tmpDic0 = dic;
        }
    }
    
    bfView.name.text = tmpDic0[@"carownname"];
    bfView.carno.text = tmpDic0[@"casecarno"];
    bfView.phone.text = tmpDic0[@"carownphone"];
    if ([tmpDic0[@"dutytype"] isEqualToString:@"0"]) {
        bfView.qzBtn.selected = YES;
    }
    
    if ([tmpDic0[@"dutytype"] isEqualToString:@"1"]) {
        bfView.wzBtn.selected = YES;
    }
    
    
    
    dfView = [[NSBundle mainBundle] loadNibNamed:@"BXBAView" owner:nil options:nil][0];
    dfView.frame = CGRectMake(0, 370, ScreenWidth, 370);
    dfView.title.text = @"对方保险信息";
    dfView.listView.dataArr = _comArr;
    [scroll  addSubview:dfView];
    
    //对方信息
//    NSDictionary *tmpDic1 = GlobleInstance.casecarlist[1];
    NSDictionary *tmpDic1;
    for (NSDictionary *dic in GlobleInstance.casecarlist) {
        if ([dic[@"party"] isEqualToString:@"0"]) {
            tmpDic1 = dic;
        }
    }
    
    dfView.name.text = tmpDic1[@"carownname"];
    dfView.carno.text = tmpDic1[@"casecarno"];
    dfView.phone.text = tmpDic1[@"carownphone"];
    if ([tmpDic1[@"dutytype"] isEqualToString:@"0"]) {
        dfView.qzBtn.selected = YES;
    }
    
    if ([tmpDic1[@"dutytype"] isEqualToString:@"1"]) {
        dfView.wzBtn.selected = YES;
    }
    
    
    NSString *owninsname = _ownDic[@"inscomname"];
    if (owninsname.length > 0) {
        [bfView.jqxBtn setTitle:owninsname forState:0];
        bfView.inscomname = owninsname;
    }
    
    NSString *owninscode = _ownDic[@"inscomcode"];
    if (owninscode.length > 0) {
        bfView.inscomcode = owninscode;
    }
    
    NSString *otherinsname = _otherDic[@"inscomname"];
    if (otherinsname.length > 0) {
        [dfView.jqxBtn setTitle:otherinsname forState:0];
        dfView.inscomname = otherinsname;
    }
    
    NSString *otherinscode = _otherDic[@"inscomcode"];
    if (otherinscode.length > 0) {
        dfView.inscomcode = otherinscode;
    }
    
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    nextBtn.frame = CGRectMake(10, dfView.maxY+30, ScreenWidth-20, 50);
    [nextBtn setTitle:@"提交" forState:0];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:0];
    nextBtn.backgroundColor = HNBlue;
    nextBtn.titleLabel.font = HNFont(16);
    [nextBtn addTarget:self action:@selector(nextBtnClicked) forControlEvents:1 << 6];
    nextBtn.layer.cornerRadius = 5;
    [scroll addSubview:nextBtn];
    
    scroll.contentSize = CGSizeMake(ScreenWidth, 850);
    [self.view addSubview:scroll];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)nextBtnClicked
{
    if (!bfView.inscomcode) {
        [Util showHudWithView:self.view message:@"请选择本方交强险承保公司" hideAfterDelay:1.0];
        return;
    }
    
    if (!dfView.inscomcode) {
        [Util showHudWithView:self.view message:@"请选择对方交强险承保公司" hideAfterDelay:1.0];
        return;
    }
    
    
    NSString *imagelon = [NSString stringWithFormat:@"%f", GlobleInstance.imagelon];
    NSString *imagelat = [NSString stringWithFormat:@"%f", GlobleInstance.imagelat];
    
    NSMutableDictionary *bean = [NSMutableDictionary dictionary];
    [bean setValue:GlobleInstance.appcaseno forKey:@"appcaseno"];
    [bean setValue:GlobleInstance.casecarno forKey:@"casecarno"];
    [bean setValue:GlobleInstance.casetelephone forKey:@"casetelephone"];
    [bean setValue:imagelon forKey:@"caselon"];
    [bean setValue:imagelat forKey:@"caselat"];
    [bean setValue:GlobleInstance.imageaddress forKey:@"caseaddress"];
    
    NSString *currentDateStr = [self getCurrentTime];
    [bean setValue:currentDateStr forKey:@"casedate"];
    
    [bean setValue:@"0" forKey:@"reportway"];
    
    [bean setValue:GlobleInstance.areaid forKey:@"areaid"];
    [bean setValue:_reportway forKey:@"type"];
    [bean setValue:GlobleInstance.userflag forKey:@"userflag"];
    [bean setValue:GlobleInstance.token forKey:@"token"];
    
    
    NSArray *tmpArr = GlobleInstance.casecarlist;
    
    NSDictionary *tmpDic0;
    for (NSDictionary *dic in tmpArr) {
        if ([dic[@"party"] isEqualToString:@"1"]) {
            tmpDic0 = dic;
        }
    }
    NSDictionary *tmpDic1;
    for (NSDictionary *dic in tmpArr) {
        if ([dic[@"party"] isEqualToString:@"0"]) {
            tmpDic1 = dic;
        }
    }
    
    
    NSMutableDictionary *bfInfo = [NSMutableDictionary dictionaryWithDictionary:tmpDic0];
    [bfInfo setValue:bfView.inscomname forKey:@"inscomname"];
    [bfInfo setValue:bfView.inscomcode forKey:@"inscomcode"];
    [bfInfo setValue:bfView.bussisinscompany forKey:@"bussisinscompany"];
    [bfInfo setValue:bfView.bussisinscomcode forKey:@"bussisinscomcode"];
    
    
    NSMutableDictionary *dfInfo = [NSMutableDictionary dictionaryWithDictionary:tmpDic1];
    [dfInfo setValue:dfView.inscomname forKey:@"inscomname"];
    [dfInfo setValue:dfView.inscomcode forKey:@"inscomcode"];
    [dfInfo setValue:dfView.bussisinscompany forKey:@"bussisinscompany"];
    [dfInfo setValue:dfView.bussisinscomcode forKey:@"bussisinscomcode"];
    
    tmpArr = @[bfInfo,dfInfo];
    [bean setValue:[Util objectToJson:tmpArr] forKey:@"casecarlist"];
    
    if ([bfInfo[@"dutytype"] isEqualToString:@"0"]) {
        inscomcode = bfInfo[@"inscomcode"];
    }
    
    if ([dfInfo[@"dutytype"] isEqualToString:@"0"]) {
        inscomcode = dfInfo[@"inscomcode"];
    }
    
    [bean setValue:inscomcode forKey:@"inscomcode"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:true];
    
    [LSHttpManager requestUrl:HNServiceURL serviceName:@"jjappsubmitCaseInforInscompany" parameters:bean complete:^(id result, ResultType resultType) {
        
        [hud hideAnimated:YES];
        if (result) {
            NSLog(@"%@", JsonResult);
        }
        
        if ([result[@"restate"] isEqualToString:@"1"]) {
            OneTipsViewController *vc = [OneTipsViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        else {
            [Util showHudWithView:self.view message:result[@"redes"] hideAfterDelay:1.0];
        }
        
    }];
}

-(NSString *)getCurrentTime
{
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //用[NSDate date]可以获取系统当前时间
    NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
    //输出格式为：2010-10-27 10:22:13
    NSLog(@"%@", currentDateStr);
    
    return currentDateStr;
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


@end
